﻿using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTab;
using HuaWei.CommClass;
using HuaWei.Common;
using HuaWei.DAI;
using HuaWei.Windows.Forms;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal static class Class28
{
    public static void JvBxsoguG(XtraTabControl xtraTabControl, [Optional, DefaultParameterValue(true)] bool _flag)
    {
        try
        {
            xtraTabControl.TabPages.Clear();
            XtraTabPage page = null;
            DataTable dtTemp = GParams._QCData.Copy();
            dtTemp.Columns.Add(new DataColumn("QCOrderNum", typeof(int)));
            foreach (DataRow dr in dtTemp.Rows)
            {
                dr["QCOrderNum"] = Convert.ToInt32(dr["QCID"]);
            }
            DataRow[] drs = dtTemp.Select("1=1", "QCOrderNum asc");
            foreach (DataRow row in drs)
            {
                QCInfo qcinfo = new QCInfo();
                qcinfo.QCID = row["QCID"].ToString();
                qcinfo.QCZT = (int)row["ZT"];
                qcinfo.JSRQ = (DateTime)row["JSRQ"];
                qcinfo.QQRQ = (DateTime)row["QQRQ"];
                AddTabPage(xtraTabControl, qcinfo);
            }
            if ((page != null) && _flag)
            {
                GParams.QCInfo = (QCInfo)page.Tag;
                xtraTabControl.SelectedTabPageIndex = xtraTabControl.TabPages.Count;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    public static void smethod(object sender, DataTableNewRowEventArgs e)
    {
        DataColumnCollection columns = e.Row.Table.Columns;
        e.Row["id"] = Guid.NewGuid();
        if (columns.Contains("XMID"))
        {
            e.Row["XMID"] = GParams._XMID;
        }
        if (columns.Contains("BDID"))
        {
            e.Row["BDID"] = GParams._BDID;
        }
        if (columns.Contains("YZID"))
        {
            e.Row["YZID"] = GParams._YZID;
        }
        if (columns.Contains("JLID"))
        {
            e.Row["JLID"] = GParams._JLID;
        }
        if (columns.Contains("QCID"))
        {
            e.Row["QCID"] = GParams.QCInfo.QCID;
        }
        if (columns.Contains("TYSB"))
        {
            e.Row["TYSB"] = 0;
        }
        if (columns.Contains("QRXD"))
        {
            e.Row["QRXD"] = 0;
        }
    }

    public static string smethod1()
    {
        return string.Format(" where bdid='{0}'", GParams._BDID);
    }

    public static void smethod10(IDataAccess idataAccess, BindingSource bindingSource)
    {
        if (GParams.DtJLQD == null)
        {
            DataSet set = new DataSet();
            set.Merge(idataAccess.ExecuteDataSet("select * from JLQD" + smethod1() + " order by qdbh", CommandType.Text, "JLQD"));
            GParams.DtJLQD = set.Tables["JLQD"];
            bindingSource.DataSource = GParams.DtJLQD;
        }
        else
        {
            bindingSource.DataSource = GParams.DtJLQD;
        }
    }

    //超计控制
    public static void smethod11(GridView gridView, RowCellStyleEventArgs rowCellStyleEventArgs)
    {
        AppearanceDefault appearanceDefault = new AppearanceDefault(Color.Red, Color.Empty, Color.Empty, Color.Empty, LinearGradientMode.Horizontal);
        AppearanceDefault default3 = new AppearanceDefault(Color.Black, Color.White, Color.Empty, Color.Empty, LinearGradientMode.Horizontal);
        if (rowCellStyleEventArgs.Column.FieldName == "SL")
        {
            DataRow dataRow = gridView.GetDataRow(rowCellStyleEventArgs.RowHandle);
            if (CHelp.ConvertInt(dataRow["xh"], 0) > 0)
            {
                CHelp.ConvertDecimal(dataRow["bl"], 0, 6);
                decimal num = CHelp.ConvertDecimal(dataRow["htsl"], 0, 3);
                decimal num2 = CHelp.ConvertDecimal(dataRow["sl"], 0, 3);
                decimal num3 = CHelp.ConvertDecimal(dataRow["zkcjbl"], 0, 0);
                if (num2 > ((num * num3) / 100M))
                {
                    AppearanceHelper.Apply(rowCellStyleEventArgs.Appearance, appearanceDefault);
                }
                else
                {
                    AppearanceHelper.Apply(rowCellStyleEventArgs.Appearance, default3);
                }
            }
        }
    }

    //超计控制取数
    public static void smethod12(DataSet dataSet, IDataAccess idataAccess, string _str, [Optional, DefaultParameterValue(false)] bool _flag)
    {
        DataTable table = null;
        if (_flag)
        {
            table = dataSet.Tables["SPQK"];
            if (table != null)
            {
                table.Clear();
            }
        }
        string str = CSQL.smethod(GParams._XMID, GParams._BDID, GParams.QCInfo.QCID, _str);
        Class39.smethod4(idataAccess, dataSet, str, "SPQK", false);
        table = dataSet.Tables["SPQK"];
        DataRow[] rowArray = table.Select(string.Format("qdbh='{0}'", _str));
        if (rowArray.Length > 0)
        {
            DataRow row;
            object obj2 = rowArray[0]["bl"];
            object obj3 = rowArray[0]["htsl"];
            if (table.Select(string.Format("xh=2 and qdbh='{0}'", _str)).Length == 0)
            {
                row = table.NewRow();
                row["xh"] = 2;
                row["qdbh"] = _str;
                row["mc"] = "本期完成";
                row["sl"] = 0;
                row["bl"] = 0;
                row["zkcjbl"] = obj2;
                row["htsl"] = obj3;
                row.EndEdit();
                table.Rows.Add(row);
            }
            if (table.Select(string.Format("xh=3 and qdbh='{0}'", _str)).Length == 0)
            {
                row = table.NewRow();
                row["xh"] = 3;
                row["qdbh"] = _str;
                row["mc"] = "本期末完成";
                row["sl"] = 0;
                row["bl"] = 0;
                row["zkcjbl"] = obj2;
                row["htsl"] = obj3;
                row.EndEdit();
                table.Rows.Add(row);
            }
        }
    }

    public static void smethod13(BindingSource bindingSource, decimal _decimal, decimal _decimal1)
    {
        decimal num = 0M;
        decimal num2 = 0M;
        DataRow row = DHelp.smethod(bindingSource, "xh", -1);
        if (row != null)
        {
            num2 = CHelp.ConvertDecimal(row["htsl"], 0, 3);
        }
        row = DHelp.smethod(bindingSource, "xh", 2);
        if (row != null)
        {
            num = CHelp.ConvertDecimal(CHelp.ConvertDecimal(row["sl"], 0, 3) + (_decimal - _decimal1), 0, 3);
            row["sl"] = num;
            if (num2 != 0M)
            {
                row["bl"] = CHelp.ConvertDecimal(num / num2, 0, 8) * 100M;
            }
            else
            {
                row["bl"] = 0;
            }
            row.EndEdit();
        }
        row = DHelp.smethod(bindingSource, "xh", 3);
        if (row != null)
        {
            num = CHelp.ConvertDecimal(CHelp.ConvertDecimal(row["sl"], 0, 3) + (_decimal - _decimal1), 0, 3);
            row["sl"] = num;
            if (num2 != 0M)
            {
                row["bl"] = CHelp.ConvertDecimal(num / num2, 0, 8) * 100M;
            }
            else
            {
                row["bl"] = 0;
            }
            row.EndEdit();
        }
        bindingSource.EndEdit();
    }

    public static string smethod14(string _str)
    {
        string[] strArray = _str.Split(new char[] { Convert.ToChar(".") });
        if (strArray.Length > 1)
        {
            return (CHelp.ToAdd0(strArray[0], 4) + "." + strArray[1]);
        }
        return CHelp.ToAdd0(_str, 3);
    }

    public static string smethod15(string _str)
    {
        return _str;
    }

    public static void SetTabPageToolTip(XtraTabPage xtraTabPage)
    {
        if (xtraTabPage.Tag is QCInfo)
        {
            QCInfo tag = (QCInfo)xtraTabPage.Tag;
            string introduced2 = tag.QQRQ.Date.ToShortDateString();
            xtraTabPage.Tooltip = string.Format("启起日期:{0} 结束日期:{1}", introduced2, tag.JSRQ.Date.ToShortDateString());
        }
    }

    public static bool ValidateQCInfo(XtraTabControl xtraTabControl, EOPLX eoplx)
    {
        QCInfo tag = (QCInfo)xtraTabControl.SelectedTabPage.Tag;
        switch (eoplx)
        {
            case EOPLX.UPLOAD:
                if (IsNotReportBefore(xtraTabControl, tag))
                {
                    MsgBox.ShowMsg("本期之前存在未上报数据，请修改后继续当前操作！");
                    return false;
                }
                break;
            case EOPLX.CANCEL:
                if (IsReportAfter(xtraTabControl, tag))
                {
                    MsgBox.ShowMsg("当期之后已存在已上报的数据，请修改后继续当前操作！");
                    return false;
                }
                break;
            case EOPLX.ROLLBACK:
                //if (!((QCInfo)xtraTabControl.SelectedTabPage.Tag).QCZT.Equals(0))
                //{
                //    MsgBox.ShowMsg("当期数据未上报，不能执行撤回操作！");
                //    return false;
                //}
                if (IsReportAfter(xtraTabControl, tag))
                {
                    MsgBox.ShowMsg("当期之后已存在已上报的数据，请修改后继续当前操作！");
                    return false;
                }
                break;

            case EOPLX.COMFIRM:
                if (IsNotApprovalBefore(xtraTabControl, tag))
                {
                    MsgBox.ShowMsg("本期之前存在数据末审批的期次，请修改后继续当前操作！");
                    return false;
                }
                break;
        }
        return true;
    }

    private static bool IsNotReportBefore(XtraTabControl xtraTabControl, QCInfo tag)
    {
        if (xtraTabControl.TabPages != null && xtraTabControl.TabPages.Count > 0)
        {
            foreach (XtraTabPage xtraTabPage in xtraTabControl.TabPages)
            {
                QCInfo info = (QCInfo)xtraTabPage.Tag;
                if (Convert.ToInt16(info.QCID) < Convert.ToInt16(tag.QCID) && info.QCZT == -1)
                {
                    return true;
                }
            }
        }
        return false;
    }

    private static bool IsReportAfter(XtraTabControl xtraTabControl, QCInfo tag)
    {
        if (xtraTabControl.TabPages != null && xtraTabControl.TabPages.Count > 0)
        {
            foreach (XtraTabPage xtraTabPage in xtraTabControl.TabPages)
            {
                QCInfo info = (QCInfo)xtraTabPage.Tag;
                if (Convert.ToInt16(info.QCID) > Convert.ToInt16(tag.QCID) && info.QCZT != -1)
                {
                    return true;
                }
            }
        }
        return false;
    }

    private static bool IsNotApprovalBefore(XtraTabControl xtraTabControl, QCInfo tag)
    {
        if (xtraTabControl.TabPages != null && xtraTabControl.TabPages.Count > 0)
        {
            foreach (XtraTabPage xtraTabPage in xtraTabControl.TabPages)
            {
                QCInfo info = (QCInfo)xtraTabPage.Tag;
                if (Convert.ToInt16(info.QCID) < Convert.ToInt16(tag.QCID) && info.QCZT != 1)
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static void smethod4(IDataAccess idataAccess, XtraTabControl xtraTabControl, int numD)
    {
        if (xtraTabControl.SelectedTabPage != null)
        {
            QCInfo tag = (QCInfo)xtraTabControl.SelectedTabPage.Tag;
            tag.QCZT = numD;
            xtraTabControl.SelectedTabPage.Tag = tag;
            GParams.QCInfo = tag;
            string sql = string.Format("update xmqc set zt={0} where bdid='{1}' and qcid='{2}'", numD, GParams._BDID, tag.QCID);
            DBHelp.ExeSql(idataAccess, sql);
            sql = string.Format("select * from {0} where bdid='{1}'", "XMQC", GParams._BDID);
            GParams._QCData = DBHelp.GetDt(idataAccess, sql, "XMQC");
        }
    }

    public static void SetTabPageTitle(XtraTabPage xtraTabPage)
    {
        QCInfo tag = (QCInfo)xtraTabPage.Tag;
        string str = "";
        switch (tag.QCZT)
        {
            case 0:
                str = "【上报】";
                break;

            case 1:
                str = "【业主】";
                break;

            default:
                str = "";
                break;
        }
        xtraTabPage.Text = "第" + tag.QCID + "期" + str;
    }

    public static void smethod6(XtraTabControl xtraTabControl)
    {
        foreach (XtraTabPage page in xtraTabControl.TabPages)
        {
            QCInfo tag = (QCInfo)page.Tag;
            DataRow[] rowArray = GParams._QCData.Select("qcid='" + tag.QCID + "'");
            tag.QCZT = CHelp.ConvertInt(rowArray[0]["zt"], 0);
            page.Tag = tag;
            SetTabPageTitle(page);
        }
    }

    public static void AddTabPage(XtraTabControl xtraTabControl, QCInfo qcinfo)
    {
        XtraTabPage xtraTabPage = null;
        xtraTabPage = xtraTabControl.TabPages.Add();
        xtraTabPage.Tag = qcinfo;
        SetTabPageTitle(xtraTabPage);
        SetTabPageToolTip(xtraTabPage);
        xtraTabControl.SelectedTabPageIndex = xtraTabControl.TabPages.Count;
    }

    public static void smethod8(IDataAccess idataAccess)
    {
        DataSet set = new DataSet();
        set.Merge(idataAccess.ExecuteDataSet("select * from GCLCT", CommandType.Text, "GCLCT"));
        GParams.DtGCLCT = set.Tables["GCLCT"];
    }

    public static void smethod9(IDataAccess idataAccess, BindingSource bindingSource)
    {
        if (GParams.DtGCLCT == null)
        {
            smethod8(idataAccess);
            bindingSource.DataSource = GParams.DtGCLCT;
        }
        else
        {
            bindingSource.DataSource = GParams.DtGCLCT;
        }
    }

    public static string YfQclfhSo(object _obj, [Optional, DefaultParameterValue("")] string _str)
    {
        string str = "";
        if (_obj == null)
        {
            return str;
        }
        string str2 = _obj.ToString();
        str = string.Format(" where bdid='{0}' and qcid={1}", GParams._BDID, str2);
        if (_str.Equals(""))
        {
            return str;
        }
        if (_str.Contains("'"))
        {
            return (str + " and pid in (" + _str + ")");
        }
        return (str + " and pid='" + _str + "'");
    }
}

